﻿using DiffuseEnjoyIdle.Core.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace System.Linq
{
    /// <summary>
    /// inc查询 数组中的每个值模糊查询
    /// </summary>
    public sealed class ExpBuilderIncMulltil : ExpressionBuilder, IExpressionBuilder
    {
        private static readonly MethodInfo MatchInfo;

        public ExpBuilderIncMulltil()
        {
            base.OpType = Enum_ConditionType.IncMul;
            base.EndKey = Enum_ConditionType.IncMul.ToString();
        }

        static ExpBuilderIncMulltil()
        {
            Type typeFromHandle = typeof(string);
            MatchInfo = typeFromHandle.GetMethod("Contains", new Type[] { typeFromHandle });
        }

        protected override Expression GenExp(Expression left, Expression right)
        {
            string[] array = (string[])((ConstantExpression)right).Value;
            Expression expression = null;
            foreach (string value in array)//遍历值 生成Contains表达式
            {
                if (string.IsNullOrEmpty(value)) continue;

                ConstantExpression constantExpression = Expression.Constant(value);
                var methodCallExpression = Expression.Call(left, MatchInfo, new Expression[] { constantExpression });
                if (expression == null)
                {
                    expression = methodCallExpression;
                }
                else
                {
                    expression = Expression.Or(expression, methodCallExpression);
                }
            }

            return expression;
        }
    }
}